#!/bin/sh
[ -z "$(pidof dnsmasq)" ] && /etc/init.d/dnsmasq reload

COMPLETE_STAT=`wifi status`

print_wifi_info() {
    local ucidev=$1 ifname=$2 index=$3 guest=$4
    local status device_json ssid psk client_count=0 enabled=0

    device_json=`echo $COMPLETE_STAT | jsonfilter -e "@.$ucidev"`
    if [ -n "$device_json" ]; then
        status=`echo $device_json | jsonfilter -e "@.disabled"`
        if [ "x$status" == "xfalse" -a -n "` echo $device_json | jsonfilter -e \"@.interfaces[${index}]\"`" ]; then
            ssid=`echo $COMPLETE_STAT | jsonfilter -e "@.$ucidev.interfaces[${index}].config.ssid"`
            psk=`echo $COMPLETE_STAT | jsonfilter -e "@.$ucidev.interfaces[${index}].config.key"`
            client_count=`iw dev $ifname station dump | grep Station | wc -l`
            enabled=1
        fi
    fi
	
    echo $ssid

    if [ $(uci get k3screenctrl.@general[0].psk_hide) -eq 1 -a $guest -eq 0 ]; then
        echo $psk | sed 's/./*/g'
    else
        echo $psk
    fi
    echo $enabled
    echo $client_count
}

echo 0 # Band mix
print_wifi_info radio0 wlan0 0 0 # 2.4GHz
print_wifi_info radio1 wlan1 0 0 # 5GHZ
print_wifi_info radio0 wlanx 1 1 # Visitor 
